package vip.xiaonuo.modular.manage.tsdatarecord.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.pojo.response.SuccessResponseData;
import vip.xiaonuo.modular.manage.tsdatarecord.entity.BaseDataType;
import vip.xiaonuo.modular.manage.tsdatarecord.mapper.BaseDataTypeMapper;
import vip.xiaonuo.modular.manage.tsdatarecord.service.BaseDataTypeService;

import java.util.List;

/**
 * BaseDataServiceImpl
 *
 * @author JackLee
 * 2022/8/25 9:47
 */
@Service
@Slf4j
public class BaseDataTypeServiceImpl extends ServiceImpl<BaseDataTypeMapper, BaseDataType> implements BaseDataTypeService {

    @Override
    public ResponseData add(BaseDataType dataType) {
        if (nameExist(dataType.getName())) {
            return new SuccessResponseData(50001, "类型名称已存在！", null);
        }
        if (codeExist(dataType.getCode())) {
            return new SuccessResponseData(50002, "类型编码已存在！", null);
        }
        dataType.setStatus(CommonStatusEnum.ENABLE.getCode());
        this.save(dataType);
        return new SuccessResponseData();
    }

    @Override
    public ResponseData delete(BaseDataType dataType) {
        BaseDataType baseDataType = this.getById(dataType.getId());
        if (ObjectUtil.isNull(baseDataType))
            return new SuccessResponseData(40001, "您删除的数据不存在！", null);
        int count = this.getBaseMapper().selectRecordCountByTypeCode(baseDataType.getCode());
        if (count > 0)
            return new SuccessResponseData(40002, "该类型下存在数据记录，不能删除！", null);
        baseDataType.setStatus(CommonStatusEnum.DELETED.getCode());
        this.updateById(baseDataType);
        return new SuccessResponseData();
    }

    @Override
    public ResponseData edit(BaseDataType dataType) {
        BaseDataType baseDataType = this.getById(dataType.getId());
        if (ObjectUtil.isNull(baseDataType))
            return new SuccessResponseData(40001, "您编辑的数据不存在！", null);
        if (StringUtils.isBlank(dataType.getName()))
            return new SuccessResponseData(50003, "类型名称不能为空！", null);

        baseDataType.setName(dataType.getName());
        baseDataType.setDisplay(dataType.getDisplay());
        this.updateById(baseDataType);

        return new SuccessResponseData();
    }

    @Override
    public List<BaseDataType> typeList(BaseDataType dataType) {
        QueryWrapper<BaseDataType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseDataType::getStatus, CommonStatusEnum.ENABLE.getCode());
        if(dataType != null){
            if(StringUtils.isNotEmpty(dataType.getName())){
                queryWrapper.lambda().like(BaseDataType::getName,dataType.getName());
            }
        }
        return this.list(queryWrapper);
    }

    @Override
    public BaseDataType getByCode(String code) {
        QueryWrapper<BaseDataType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseDataType::getCode, code).eq(BaseDataType::getStatus, CommonStatusEnum.ENABLE.getCode());
        return this.getOne(queryWrapper);
    }

    private boolean nameExist(String name) {
        if (StringUtils.isBlank(name))
            return false;
        QueryWrapper<BaseDataType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseDataType::getStatus, CommonStatusEnum.ENABLE.getCode()).eq(BaseDataType::getName, name);
        return this.count(queryWrapper) > 0;
    }

    private boolean codeExist(String code) {
        if (StringUtils.isBlank(code))
            return false;
        QueryWrapper<BaseDataType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseDataType::getStatus, CommonStatusEnum.ENABLE.getCode()).eq(BaseDataType::getCode, code);
        return this.count(queryWrapper) > 0;
    }
}
